import BasicController from './../BasicController'
import ErrorHandlerCode from './../../util/ErrorHandlerCode'
import {ArticletypeModel} from '../../model'

class ArticletypeController extends BasicController{
    insert(req,res){
        if(!super.isEmptyParameter(req.body.type_name)){
            res.json({
                code:ErrorHandlerCode.MISSINGPARAMETER,
                message: 'type_name 为必填参数'
            })
            return;
        }
        if(!super.isEmptyParameter(req.body.remark)){
            res.json({
                code:ErrorHandlerCode.MISSINGPARAMETER,
                message: 'remark 为必填参数'
            })
            return;
        }
        if(!super.isEmptyParameter(req.body.is_publish)){
            res.json({
                code:ErrorHandlerCode.MISSINGPARAMETER,
                message: 'is_publish 为必填参数'
            })
            return;
        }
        ArticletypeModel.insertType(req.body).then((data) =>{
            super.RequestSuccessMsg(res, data);
        }).catch((err) => {
            res.json(ErrorHandlerCode.INNERSERVICERROR);
            throw err;
        })
    }
    delArticletype(req,res){
        if(!super.isEmptyParameter(req.body.id)){
            res.json({
                code:ErrorHandlerCode.MISSINGPARAMETER,
                message: 'id 为必填参数'
            })
            return;
        }
        ArticletypeModel.delTypeById(req.body.id).then((data) =>{
            super.RequestSuccessMsg(res, data);
        }).catch((err) => {
            res.json(ErrorHandlerCode.INNERSERVICERROR);
            throw err;
        })
    }
    listpageArticletype(req,res){
        if(!super.isEmptyParameter(req.body.page_size)){
            req.body.page_size = 10;
        }
        if(!super.isEmptyParameter(req.body.page_no)){
            req.body.page_no = 1;
        }
        ArticletypeModel.pagingType(req.body).then((data) =>{
            super.RequestSuccessMsg(res, data);
        }).catch((err) => {
            res.json(ErrorHandlerCode.INNERSERVICERROR);
            throw err;
        })
    }
    updateArticletype(req,res){
        if(!super.isEmptyParameter(req.body.id)){
            res.json({
                code:ErrorHandlerCode.MISSINGPARAMETER,
                message: 'id 为必填参数'
            })
            return;
        }
        ArticletypeModel.updateType(req.body).then((data) =>{
            super.RequestSuccessMsg(res, data);
        }).catch((err) => {
            res.json(ErrorHandlerCode.INNERSERVICERROR);
            throw err;
        })
    }
    findPublishList(req,res){
        if(!super.isEmptyParameter(req.body.is_publish)){
            res.json({
                code:ErrorHandlerCode.MISSINGPARAMETER,
                message: 'is_publish 为必填参数'
            });
            return;
        }
        ArticletypeModel.findListPublish().then((data) =>{
            super.RequestSuccessMsg(res, data);
        }).catch((err) => {
            res.json(ErrorHandlerCode.INNERSERVICERROR);
            throw err;
        })
    }
    findAllTypeList(req,res){
        ArticletypeModel.findAllType().then((data) =>{
            super.RequestSuccessMsg(res, data);
        }).catch((err) => {
            res.json(ErrorHandlerCode.INNERSERVICERROR);
            throw err;
        })
    }
}
module.exports = new ArticletypeController();